package com.nimt.hqgl_idea.service;

import com.nimt.hqgl_idea.entity.Merchant;
import com.nimt.hqgl_idea.mapper.MerchantMapper;
import com.nimt.hqgl_idea.param.MerchantParam;
import com.nimt.hqgl_idea.util.ErrorEnum;
import com.nimt.hqgl_idea.util.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    MerchantMapper merchantMapper;

    @Override
    public ResponseBody query(MerchantParam merchantParam) {
        ResponseBody responseBody = null;
        merchantParam.setTotalCount(merchantMapper.count(merchantParam.getKeywords()));
        int start = (merchantParam.getCurrentPage()-1)*merchantParam.getPageSize();
        List<Merchant> merchants = merchantMapper.selectAll(start,merchantParam.getPageSize(),merchantParam.getKeywords());
        if (merchants == null || merchants.size() == 0){
            responseBody = new ResponseBody(ErrorEnum.No_More_Data);
        }else {
            merchantParam.setData(merchants);
            responseBody  = new ResponseBody(merchantParam);
        }
        return responseBody;
    }

    @Override
    public ResponseBody state(Merchant merchant){
        merchantMapper.update(merchant);
        return new ResponseBody("操作成功！");
    }

    @Override
    public ResponseBody updateMerchant(Merchant merchant){
        ResponseBody responseBody = null;
        Merchant merchantOne = merchantMapper.selectById(merchant.getId());
        if (merchantOne != null) {
            merchantMapper.update(merchant);
            responseBody = new ResponseBody(merchant);
        } else {
            responseBody = new ResponseBody("商家不存在！");
        }
        return responseBody;
    }

    @Override
    public ResponseBody del(Integer id){
        merchantMapper.delete(id);
        return new ResponseBody("删除成功！");
    }

    @Override
    public ResponseBody add(Merchant merchant){
        ResponseBody responseBody = null;
        merchantMapper.add(merchant);
        if (merchant.getId() == null){
            responseBody = new ResponseBody(ErrorEnum.Insert_Faild);
        }else {
            responseBody = new ResponseBody(merchant);
        }
        return responseBody;
    }
}
